<?php


namespace App\Zesow\Cache\Impl;

use Illuminate\Support\Facades\Cache;
use App\Services\Business\Interfaces\MediaCategoryServiceInterface;

class MediaCategoryCache
{

    public const KEY_NAME = 'media_category';

    private $mediaCategoryService;

    public function __construct(MediaCategoryServiceInterface $mediaCategoryService)
    {
        $this->mediaCategoryService = $mediaCategoryService;
    }

    public function tree(int $shopId)
    {

        if (Cache::has(self::KEY_NAME)) {
            return Cache::get(self::KEY_NAME);
        }
        $shopCategories = $this->mediaCategoryService->tree($shopId);
        Cache::put(self::KEY_NAME, $shopCategories);
        return $shopCategories;


    }

    public function destroy(int $shopId): void
    {
        Cache::forget($this->key($shopId));
    }

    private function key(int $shopId): string
    {
        return self::KEY_NAME . '_' . $shopId;
    }

}
